{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 05\n",
    "# 列表索引和切片\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表长度为\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "# 创建一个混合列表\n",
    "my_list = [1, 1.0, '1', True, [1, 1.0, '1'], {1}, {1: 1.0}]\n",
    "print('列表长度为')\n",
    "print(len(my_list))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:02:24.135632Z",
     "end_time": "2024-07-02T16:02:24.144332Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "元素：1，索引：0，类型：<class 'int'>\n",
      "元素：1.0，索引：1，类型：<class 'float'>\n",
      "元素：1，索引：2，类型：<class 'str'>\n",
      "元素：True，索引：3，类型：<class 'bool'>\n",
      "元素：[1, 1.0, '1']，索引：4，类型：<class 'list'>\n",
      "元素：{1}，索引：5，类型：<class 'set'>\n",
      "元素：{1: 1.0}，索引：6，类型：<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# 打印每个元素和对应的序号\n",
    "for index, item in enumerate(my_list):\n",
    "    type_i = type(item)\n",
    "    print(f\"元素：{item}，索引：{index}，类型：{type_i}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:02:37.919921Z",
     "end_time": "2024-07-02T16:02:37.927940Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1.0\n"
     ]
    }
   ],
   "source": [
    "# 列表索引\n",
    "print(my_list[0])\n",
    "print(my_list[1])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:02:58.684323Z",
     "end_time": "2024-07-02T16:02:58.692877Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 1.0}\n",
      "{1}\n"
     ]
    }
   ],
   "source": [
    "print(my_list[-1])\n",
    "print(my_list[-2])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:03:05.606087Z",
     "end_time": "2024-07-02T16:03:05.615402Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1.0, '1']\n"
     ]
    }
   ],
   "source": [
    "# 列表切片\n",
    "# 取出前3个元素，索引为0、1、2\n",
    "print(my_list[:3])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:03:13.294730Z",
     "end_time": "2024-07-02T16:03:13.304265Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.0, '1', True]\n"
     ]
    }
   ],
   "source": [
    "# 取出索引1、2、3，不含0，不含4\n",
    "print(my_list[1:4])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:03:20.395065Z",
     "end_time": "2024-07-02T16:03:20.408010Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, '1', [1, 1.0, '1'], {1: 1.0}]\n"
     ]
    }
   ],
   "source": [
    "# 指定步长2，取出第0、2、4、6\n",
    "print(my_list[::2])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:03:27.020667Z",
     "end_time": "2024-07-02T16:03:27.030169Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{1: 1.0}, {1}, [1, 1.0, '1'], True, '1', 1.0, 1]\n"
     ]
    }
   ],
   "source": [
    "# 指定步长-1，倒序\n",
    "print(my_list[::-1])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:03:34.110892Z",
     "end_time": "2024-07-02T16:03:34.122320Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n"
     ]
    }
   ],
   "source": [
    "# 提取列表中的列表某个元素\n",
    "print(my_list[4][1])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:03:41.877376Z",
     "end_time": "2024-07-02T16:03:41.887947Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
